package MyGui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import DistSys.*;

/**
 * Title:        iPAD
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:      ABB Inc.
 * @author Fangxing Li
 * @version 1.0
 */

public class LayerMngrDlg extends MyDlgFrame implements ActionListener, WindowListener{

  private static final int NUM_LAYER = Sys.prjInfo.NUM_LAYER;

  private JLabel lblNum, lblDesc, lblMode, lblActive;
  private JLabel[] alblNum;
  private JTextField[] atfDesc;
  private JButton[] abtnMode;
  private ButtonGroup btnGroup;
  private JRadioButton[] arbActive;
  private JButton btnRefresh, btnAllOn, btnAllGray, btnAllOff;

  public LayerMngrDlg(MainFrame parent, String title) {
      super( parent, title );
      init();
  }

  public void init(){
      lblNum = new JLabel("#"); lblNum.setFont( GuiConst.fontDefault );
      lblDesc = new JLabel("Description");  lblDesc.setFont( GuiConst.fontDefault );
      lblMode = new JLabel("View"); lblMode.setFont( GuiConst.fontDefault );
      lblActive=new JLabel(" "); //no lable at all?

      alblNum = new JLabel[NUM_LAYER];
      atfDesc = new JTextField[NUM_LAYER];
      abtnMode= new JButton[NUM_LAYER];
      arbActive=new JRadioButton[NUM_LAYER];
      btnGroup = new ButtonGroup();

      for(int i=0; i<NUM_LAYER; i++){
          alblNum[i]= new JLabel(" ");
          alblNum[i].setFont( GuiConst.fontDefault );
          atfDesc[i]  = new JTextField("", 8);
          atfDesc[i].setFont( GuiConst.fontDefault );
          abtnMode[i] = new JButton( Layer.STR_MODE[0] );  //dft label: On
          abtnMode[i].setFont( GuiConst.fontDefault );
          arbActive[i]= new JRadioButton();
          btnGroup.add( arbActive[i] );
          if( i >= ProjectInfo.NUM_BACK_GND_LAYER){
              alblNum[i].setText(""+ (i-ProjectInfo.NUM_BACK_GND_LAYER) );
          }
      }
      atfDesc[0].setText("Images");
      atfDesc[0].setEnabled(false);
      atfDesc[1].setText("Background");
      atfDesc[1].setEnabled(false);

      btnRefresh = new JButton("Refresh Drawing");  btnRefresh.setFont(GuiConst.fontDefault);
      btnAllOn = new JButton(" All On ");   btnAllOn.setFont(GuiConst.fontDefault);
      btnAllGray = new JButton("All Gray"); btnAllGray.setFont(GuiConst.fontDefault);
      btnAllOff = new JButton("All Off ");  btnAllOff.setFont(GuiConst.fontDefault);

      GridBagLayout gridBag = new GridBagLayout();
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.insets = new Insets(2,2,2,2);
      gbc.fill = GridBagConstraints.BOTH;
      gbc.anchor =GridBagConstraints.WEST;
      gbc.gridheight = 1;
      gbc.gridwidth  = 1;
      gbc.weighty =1;

      Container pane = this.getContentPane();
      pane.setLayout( gridBag);
      gbc.gridx = 0;  gbc.gridy = 0;  pane.add( lblNum, gbc);
      gbc.gridx = 1;  gbc.gridy = 0;  pane.add( lblDesc, gbc);
      gbc.gridx = 2;  gbc.gridy = 0;  pane.add( lblMode, gbc);

      for(int i=0; i<NUM_LAYER; i++){
          gbc.gridy = i+1;
          gbc.weightx =0; gbc.gridx = 0;  pane.add( alblNum[i], gbc);
          gbc.weightx =1; gbc.gridx = 1;  pane.add( atfDesc[i], gbc);
          gbc.weightx =0; gbc.gridx = 2;  pane.add( abtnMode[i], gbc);
          gbc.weightx =0; gbc.gridx = 3;  pane.add( arbActive[i], gbc);
      }

      gbc.gridy +=1; gbc.gridx = 0; gbc.gridheight = 3; gbc.gridwidth = 2;
      pane.add( btnRefresh, gbc);

                    gbc.gridx = 2; gbc.gridheight = 1; gbc.gridwidth = 1;
      pane.add( btnAllOn, gbc);

      gbc.gridy +=1; gbc.gridx = 2; gbc.gridheight = 1; gbc.gridwidth = 1;
      pane.add( btnAllGray, gbc);

      gbc.gridy +=1; gbc.gridx = 2; gbc.gridheight = 1; gbc.gridwidth = 1;
      pane.add( btnAllOff, gbc);

      this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//this is by default
      this.setSize(230,520);  //(300,250) ==>
      this.setLocation(200,200);

      this.addWindowListener(this);
      btnRefresh.addActionListener( this );
      btnAllOn.addActionListener( this );
      btnAllOff.addActionListener( this );
      btnAllGray.addActionListener( this );

      for(int i=0; i< NUM_LAYER; i++){
          atfDesc[i].addFocusListener( flTfDesc);
          abtnMode[i].addActionListener( alBtnMode );
          arbActive[i].addActionListener( alActive);
      }
  }

  //implement ActionListener
  public void actionPerformed( ActionEvent evt ){
      Object src = evt.getSource();
      if( src == this.btnRefresh ){
          mainFrame.mainPanel.needRedraw=true;
          mainFrame.mainPanel.repaint();
      }else if( src == this.btnAllOn ) {
          for( int i=0; i< NUM_LAYER; i++ ){
              Layer layer= (Layer) Sys.prjInfo.layerList.get(i);
              layer.setMode( Layer.ON );
              abtnMode[i].setText( Layer.STR_MODE[ Layer.ON ] );
          }
      }else if( src == this.btnAllOff ) {
          for( int i=0; i< NUM_LAYER; i++ ){
              Layer layer= (Layer) Sys.prjInfo.layerList.get(i);
              layer.setMode( Layer.OFF );
              abtnMode[i].setText( Layer.STR_MODE[ Layer.OFF ] );
          }
      }else if( src == this.btnAllGray ) {
          for( int i=0; i< NUM_LAYER; i++ ){
              Layer layer= (Layer) Sys.prjInfo.layerList.get(i);
              layer.setMode( Layer.GRAY );
              abtnMode[i].setText( Layer.STR_MODE[ Layer.GRAY ] );
          }
      }
  }//end of actionPerformed

  /*======================================
    Implement WindowListener interfaces
    ======================================*/
  public void windowOpened(WindowEvent evt) { }
  public void windowClosed(WindowEvent evt) { }
  public void windowIconified(WindowEvent evt) { }
  public void windowDeiconified(WindowEvent evt) { }
  public void windowActivated(WindowEvent evt) {
      updateText();
  }
  public void windowDeactivated(WindowEvent evt) { }
  public void windowClosing(WindowEvent evt) { }

  private void updateText(){
      int iActiveLayer = Sys.prjInfo.iActiveLayer + ProjectInfo.NUM_BACK_GND_LAYER;
      arbActive[iActiveLayer].setSelected( true );

      Layer layer;
      int mode;
      int sz = Sys.prjInfo.layerList.size();
      sz = Math.min( sz, NUM_LAYER);//preventative redundency; should be the same
      for(int i=0; i< sz; i++ ){
          layer = (Layer) Sys.prjInfo.layerList.get(i);
          if( i>=ProjectInfo.NUM_BACK_GND_LAYER ) {
              atfDesc[i].setText( layer.getName() ); }
          mode = layer.getMode();
          abtnMode[i].setText( Layer.STR_MODE[mode] );
      }
  }

  /**
   * Used by atfDesc[i]
   */
  FocusListener flTfDesc = new FocusListener(){
      public void focusGained(FocusEvent evt){ }
      public void focusLost(FocusEvent evt){
          JTextField src = (JTextField) evt.getSource();
          for(int i=0; i< NUM_LAYER; i++ ){
              if( src == atfDesc[i] ){
                  Layer layer= (Layer) Sys.prjInfo.layerList.get(i);
                  layer.setName( atfDesc[i].getText() );
                  return;
              }
          }//end of for loop
      }//end of focusLost
  };//end of FocusListener

  ActionListener alBtnMode = new ActionListener(){
      public void actionPerformed(ActionEvent evt){
          JButton src = (JButton) evt.getSource();
          for( int i=0; i< NUM_LAYER; i++){
              if( src == abtnMode[i] ) {
                  Layer layer= (Layer) Sys.prjInfo.layerList.get(i);
                  int mode = layer.getMode();
                  mode = (mode +2)%3;     // On-Gray-Off; 0-2-1;
                  layer.setMode( mode );  //save mode
                  abtnMode[i].setText( Layer.STR_MODE[mode] );
                  return;
              }
          }
      }//end of actionPerformed():alBtnMode
  };// end of new ActionListener():alBtnMode

  ActionListener alActive = new ActionListener(){
      public void actionPerformed(ActionEvent evt){
          Window win;
          JRadioButton src = (JRadioButton) evt.getSource();
          for( int i=0; i< NUM_LAYER; i++){
              if( src == arbActive[i] ) {
                  int iSelectedLayer = (i-ProjectInfo.NUM_BACK_GND_LAYER);
                  //If active layer is changed AND neither prev. nor curr. layer
                  //is a foreground layer, then reset redo/undo queue
                  if( Sys.prjInfo.iActiveLayer != iSelectedLayer ) {
                      if( iSelectedLayer == ProjectInfo.BG_IMAGE_LAYER ){
                          mainFrame.menuBar.activeQueue =
                             mainFrame.menuBar.editActionQueueBgImage;
                      } else if( iSelectedLayer == ProjectInfo.BG_LINE_LAYER ) {
                          mainFrame.menuBar.activeQueue =
                             mainFrame.menuBar.editActionQueueBgLine;
                      } else { //foreground layers
                          if( Sys.prjInfo.iActiveLayer <0 ){
                              mainFrame.menuBar.activeQueue =
                                  mainFrame.menuBar.editActionQueueFg;
                          }
                      }
                      mainFrame.menuBar.setUndoEnabled(
                          mainFrame.menuBar.activeQueue.canUndo());
                      mainFrame.menuBar.setRedoEnabled(
                          mainFrame.menuBar.activeQueue.canRedo());
                  }
                  Sys.prjInfo.iActiveLayer = iSelectedLayer;
                  break;
              }
          }

          if( Sys.prjInfo.iActiveLayer == ProjectInfo.BG_IMAGE_LAYER){
              //background images
              mainFrame.menuBar.setLoadBgImageEnabled(true);
              mainFrame.btnPanel.enterBgImageMode();
              win = mainFrame.menuBar.aIpadDlg[MainMenuBar.EDIT_BGLINE_DLG];
              if(win != null){ win.setVisible(false); }
              win = mainFrame.menuBar.aIpadDlg[MainMenuBar.ELEM_INFO_DLG];
              if(win != null){ win.setVisible(false); }
          } else if( Sys.prjInfo.iActiveLayer == ProjectInfo.BG_LINE_LAYER ) {
              //background lines
              mainFrame.menuBar.setLoadBgImageEnabled(false);
              mainFrame.btnPanel.enterBgLineMode();
              win = mainFrame.menuBar.aIpadDlg[MainMenuBar.EDIT_BGIMAGE_DLG];
              if(win != null){ win.setVisible(false); }
              win = mainFrame.menuBar.aIpadDlg[MainMenuBar.ELEM_INFO_DLG];
              if(win != null){ win.setVisible(false); }
          } else {
              //foreground layers
              mainFrame.menuBar.setLoadBgImageEnabled(false);
              mainFrame.btnPanel.enterForeGroundMode();
              win = mainFrame.menuBar.aIpadDlg[MainMenuBar.EDIT_BGIMAGE_DLG];
              if(win != null){ win.setVisible(false); }
              win = mainFrame.menuBar.aIpadDlg[MainMenuBar.EDIT_BGLINE_DLG];
              if(win != null){ win.setVisible(false); }
          }
          Sys.clearSelectionAndFocus();
          mainFrame.mainPanel.needRedraw=true;
          mainFrame.mainPanel.repaint();
      }//end of actionPerformed():alActive
  };// end of new ActionListener():alActive

}